<?php
/**
 * ShopCartModel.class.php
 * 购物车模型
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-02-23
 */
namespace Admin\Model;
use Think\Model;

class ShopCartModel extends Model{
	protected 	$trueTableName 	= 't_shop_cart';
	public 	$errMsg 	= false;

	/**
	 * 获取购物车列表
	 * @param   $page 页码数
	 * @param   $pageHtml 引用html页码
	 * @return  $list
	 */
	function getList($page, &$pageHtml){
		$list 	= $this->table('t_shop_cart a')
			->join('left join t_user b on a.user_id = b.id')
			->page($page, C('PAGE_COUNT'))
			->cache(true)
			->order('a.id desc')
			->field('a.id, a.user_id, b.nickname, a.goods_type, a.goods_ids, a.goods_infos,
				a.amount, a.create_time, a.status')
			->select();

		foreach($list as &$item){
			switch($item['goods_type']){
				case 1:
					$item['goods_type_text']	= '在线课程';
					$courseIds 	.= $item['goods_ids'].',';
				break;
			}
			$item['goods_infos']	= json_decode($item['goods_infos'], true);
			$item['create_date']	= date('Y-m-d', $item['create_time']);
			$item['status_text']	= C('SHOPCART_STATUS.'.$item['status']);
		}
		if($courseIds){
			$courseIds 	= explode(',', $courseIds);
			$courseIds 	= array_unique($courseIds);
			foreach($courseIds as $key => $val){
				if(!$val)	unset($courseIds[$key]);
			}
			$courseIds 	= implode(',', $courseIds);
			$courses 	= M('t_course', null)->where('id in (%s)', $courseIds)->getField('id, name');
		}
		foreach($list as &$item){
			switch($item['goods_type']){
				case 1:
					$cIds 	= explode(',', $item['goods_ids']);
					foreach($cIds as $id){
						$item['goods_name']	.= msubstr($courses[$id], 0, 4).'|';
					}
					$item['goods_name']	= msubstr($item['goods_name'], 0, 20, 'utf-8', false);
					$item['goods_count']= count($cIds);
				break;
			}
		}
		
		$count 	= $this->cache(true)->count();
		$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
		$pageHtml 	= $Page->angular_show();
		
		return $list;
	}

	/**
	 * 获取交易列表
	 * @param   $page 页码数
	 * @param   $keyword 关键词
	 * @param   $timeS 起始时间
	 * @param   $timeE 结束时间
	 * @param   $pageHtml 引用HTML分页元素
	 * @return  $list [<description>]
	 */
	function getBargainList($keyword, $timeS, $timeE, $page, &$pageHtml, &$total){
		$where 	= 'a.status = 1';
		if($keyword)	$where 	.= sprintf(' and b.nickname like "%s%%"', $keyword);
		if($timeS)	$where 	.= sprintf(' and a.create_time >= %d', $timeS);
		if($timeE)	$where 	.= sprintf(' and a.create_time <= %d', $timeE);

		$list 	= $this->table('t_shop_cart a')
			->join('left join t_user b on a.user_id = b.id')
			->join('left join t_money c on a.money_id = c.id')
			->page($page, C('PAGE_COUNT'))
			->where($where)
			->field('a.id, a.user_id,a.goods_type, a.goods_ids, a.goods_infos, a.create_time, a.status, a.price, 
				b.nickname, b.truename, c.id money_id, c.amount, c.deduct, c.trade_no')
			->cache(true)
			->select();
		foreach($list as &$item){
			$item['goods_type_text']	= $item['goods_type'] == 1 ? '在线课程' : '未知';
			$item['profit']	= $item['amount']	- $item['deduct'];
			$item['create_date']	= date('Y-m-d', $item['create_time']);
			$item['goods_names'] 	= array();
			$item['goods_infos'] 	= json_decode($item['goods_infos'], true);
			foreach($item['goods_infos'] as $goodsInfo){
				$item['goods_names'][] 	= $goodsInfo['name'];
			}
			$item['goods_names']	= implode(',', $item['goods_names']);
			$item['sim_goods_names']= msubstr($item['goods_names'], 0, 10);
		}

		//获取分页html
		if($pageHtml){
			$count = $this->table('t_shop_cart a')
				->join('left join t_user b on a.user_id = b.id')
				->where($where)
				->cache(true)
				->count();
			$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		//获取合计信息
		if($total){
			$total 	= $this->table('t_shop_cart a')
				->join('left join t_user b on a.user_id = b.id')
				->join('left join t_money c on a.money_id = c.id')
				->where($where)
				->field('count(a.id) count, sum(c.amount) amount, sum(c.deduct) deduct')
				->cache(true)
				->find();
			$total['profit']	= $total['amount'] - $total['deduct'];
		}	
		return $list;
		
	}

	/**
	 * 导出Excel
	 * @param   $start 	开始位置
	 * @param   $lenght 到处长度
	 * @param   $keyword 关键词
	 * @param   $timeS 起始时间
	 * @param   $timeE 结束时间
	 * @return  list [<description>]
	 */
	function outBargainExcel($start, $length, $keyword, $timeS, $timeE){
		$where 	= 'a.status = 1';
		if($keyword)	$where 	.= sprintf(' and b.nickname like "%s%%"', $keyword);
		if($timeS)		$where 	.= sprintf(' and a.create_time >= %d', $timeS);
		if($timeE)		$where 	.= sprintf(' and a.create_time <= %d', $timeE);

		if($start === false){
			$count = $this->table('t_shop_cart a')
				->join('left join t_user b on a.user_id = b.id')
				->where($where)
				->cache(true)
				->count();
			return $count;
		}

		$list 	= $this->table('t_shop_cart a')
			->join('left join t_user b on a.user_id = b.id')
			->join('left join t_money c on a.money_id = c.id')
			->join('left join t_coupon_code d on c.code_id = d.id')
			->page($page, $length)
			->where($where)
			->field('a.id, a.goods_type, a.goods_ids, a.goods_infos, a.create_time, a.status, a.price, 
				b.nickname, b.truename, b.role_id, c.id money_id, c.amount, c.deduct, c.trade_no,
				d.code, d.amount code_amount')
			->cache(true)
			->select();
		foreach($list as &$item){
			$item['goods_type_text']	= $item['goods_type'] == 1 ? '在线课程' : '未知';
			$item['profit']	= $item['amount']	- $item['deduct'];
			$item['create_date']	= date('Y-m-d', $item['create_time']);
			$item['goods_names'] 	= array();
			$item['goods_infos'] 	= json_decode($item['goods_infos'], true);
			foreach($item['goods_infos'] as $goodsInfo){
				$item['goods_names'][] 	= $goodsInfo['name'];
			}
			$item['goods_names']	= implode('|', $item['goods_names']);
			$item['sim_goods_names']= msubstr($item['goods_names'], 0, 10);
			$item['role_text']	= FUserModel::ROLE_MAP[$item['role_id']];
		}

		return $list;	
	}

	/**
	 * 获取交易详情
	 * @param   $shopcartId 购物车ID
	 * @return  $info [<description>]
	 */
	function detailBargainInfo($shopcartId){
		$moneyInfo 	= $this->table('t_shop_cart a')
			->join('left join t_money b on a.money_id = b.id')
			->join('left join t_coupon_code c on b.code_id = c.id')
			->where('a.id = %d', $shopcartId)
			->cache(true)
			->field('a.goods_infos, a.goods_ids, a.create_time, a.status,
				b.id money_id, b.trade_no, b.amount, b.deduct, c.code, c.amount code_amount')
			->find();
		// dump($moneyInfo);
		$deductList 	= M('t_deduct a', null)
			->join('left join t_user b on a.user_id = b.id')
			->where('a.money_id = %d', $moneyInfo['money_id'])
			->field('a.user_id, a.money_id, a.amount, a.percent, b.nickname, b.truename,b.role_id')
			->select();

		foreach($deductList as &$item){
			$item['role_text']	= FUserModel::ROLE_MAP[$item['role_id']];
		}

		return array('money_info' => $moneyInfo, 'deduct_list' => $deductList);
	}
}